<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Main_Model extends CI_Model {
	
	public function pageview() {
		$nowDay = date('Y-m-d');

		$this->db->where('date', $nowDay);

		$this->db->from('pageview');

		$result = $this->db->count_all_results();

		$NowHour = date('H');

		if((substr($NowHour, 0,1)) == '0'){
			$NowHour = substr($NowHour, 1, 1);
		}
		$nowField = 'count_' . $NowHour;
			
		if($result){

			//Set Setting
			$this->db->set($nowField, $nowField.' + 1' , FALSE);
			$this->db->set('total', 'total + 1' , FALSE);

			$this->db->where('date', $nowDay);

			//Table Setting
			$this->db->update('pageview');
		}
		else{
			$fields = array(
					'date' => $nowDay,
					$nowField => '1',
					'total' => '1'
			);

			//Set Setting
			$this->db->set($fields);

			//Table Setting
			$this->db->insert('pageview');
		}
	}
	public function main() {
	
		$this->db->order_by('sort', 'asc');
		$query = $this->db->get('main');
	
		$rows = $query->result_array();
	
		return $rows;
	}
	
	public function cover() {
	
		$this->db->order_by('sort', 'asc');
		$query = $this->db->get('cover');
	
		$rows = $query->result_array();
	
		return $rows;
	}
	
	public function cover_total_rows() {
	
		$total_rows = $this->db->count_all('cover');
	
		return $total_rows;
	}
	
	public function view_count($mode) {
	
		$this->db->where('id', 1);
	
		$query = $this->db->get('view_count');
		$row = $query->row_array();
	
		return $row[$mode];
	}
	
	public function work($category_id, $search, $limit, $offset) {
	
		if($search) {
			$this->db->like('title1', $search);
			$this->db->or_like('title2', $search);
			$this->db->or_like('tag', $search);
			$this->db->or_like('content', $search);
		}
		elseif($category_id != 'all') $this->db->like('category_id', '"'.$category_id.'"');
		
		$this->db->order_by('sort', 'asc');
		$this->db->limit($limit, $offset);
		$query = $this->db->get('work');
	
		$rows = $query->result_array();
	
		return $rows;
	}
	
	public function work_total_rows($category_id, $search) {
	
		if($search) {
			$this->db->like('title1', $search);
			$this->db->or_like('title2', $search);
			$this->db->or_like('tag', $search);
			$this->db->or_like('content', $search);
		}
		elseif($category_id != 'all') $this->db->like('category_id', '"'.$category_id.'"');
		
		$this->db->select('count(id) as count', false);
		
		$query = $this->db->get('work');
		$row = $query->row_array();
		
		return $row['count'];
	}
	
	public function work_view($id) {
	
		$this->db->where('id', $id);
	
		$query = $this->db->get('work');
	
		$row = $query->row_array();
	
		return $row;
	}
	
	public function work_view_new($category_id, $search, $sort) {
	
		if($search) $this->db->where('(title1 like "%'.$search.'%" OR title2 like "%'.$search.'%" OR tag like "%'.$search.'%" OR content like "%'.$search.'%")', NULL, FALSE);
		elseif($category_id != 'all') $this->db->like('category_id', '"'.$category_id.'"');
		
		$this->db->where('sort <', $sort);
		$this->db->where('security', 'open');
		$this->db->order_by('sort', 'desc');
		$this->db->limit(1);
		
		$query = $this->db->get('work');
	
		$row = $query->row_array();
	
		return $row;
	}
	
	public function work_view_old($category_id, $search, $sort) {
		
		if($search) $this->db->where('(title1 like "%'.$search.'%" OR title2 like "%'.$search.'%" OR tag like "%'.$search.'%" OR content like "%'.$search.'%")', NULL, FALSE);
		elseif($category_id != 'all') $this->db->like('category_id', '"'.$category_id.'"');
		
		$this->db->where('sort >', $sort);
		$this->db->where('security', 'open');
		$this->db->order_by('sort', 'asc');
		$this->db->limit(1);
	
		$query = $this->db->get('work');
	
		$row = $query->row_array();
	
		return $row;
	}
	
	public function category_rows() {
	
		$this->db->order_by('sort', 'asc');
		$query = $this->db->get('category');
	
		$rows = $query->result_array();
	
		return $rows;
	}
	
	public function blog_view($id) {
	
		$this->db->where('id', $id);
	
		$query = $this->db->get('blog');
	
		$row = $query->row_array();
	
		return $row;
	}
	
	public function blog($category_id, $limit, $offset) {
	
		if($category_id != 'all') $this->db->like('category_id', '"'.$category_id.'"');
		
		$this->db->order_by('sort', 'asc');
		$this->db->limit($limit, $offset);
		$query = $this->db->get('blog');
	
		$rows = $query->result_array();
	
		return $rows;
	}
	
	public function blog_total_rows($category_id) {
	
		if($category_id != 'all') $this->db->like('category_id', '"'.$category_id.'"');
	
		$this->db->select('count(id) as count', false);
	
		$query = $this->db->get('blog');
		$row = $query->row_array();
	
		return $row['count'];
	}
	
	public function blog_category_rows() {
	
		$this->db->order_by('sort', 'asc');
		$query = $this->db->get('blog_category');
	
		$rows = $query->result_array();
	
		return $rows;
	}
	
	public function blog_archive_rows($category_id) {
		
		if($category_id != 'all') $this->db->like('category_id', '"'.$category_id.'"');
		
		$this->db->order_by('date_Y', 'desc');
		$this->db->group_by('date_Y');
		
		$this->db->select('date_format(date, "%Y") as date_Y', false);
		$this->db->select('count(*) as count', false);
		
		$query = $this->db->get('blog');
		
		$rows = $query->result_array();
		
		return $rows;
	}
	
	public function project_request_write_action($company_name, $user_name, $email_address, $phone_number, $kind, $budget, $schedule, $comment, $image) {
		
		$kind = json_encode($kind);
		$image = json_encode($image);
		
		$fields = array(
				'company_name' => $company_name,
				'user_name' => $user_name,
				'email_address' => $email_address,
				'phone_number' => $phone_number,
				'kind' => $kind,
				'budget' => $budget,
				'schedule' => $schedule,
				'image' => $image,
				'comment' => $comment,
				'register_date' => date('Y-m-d H:i:s')
		);
		$this->db->set($fields);
		
		$this->db->insert('project_request');
	}
	
	public function careers_view() {
	
		$this->db->where('view', 'show');
	
		$query = $this->db->get('careers');
	
		$row = $query->row_array();
	
		return $row;
	}
}

/* End of file main_model.php */
/* Location: ./application/models/main_model.php */